package triggerhelper.controllers;

import javax.swing.*;
import triggerhelper.dataHolders.Trigger;
import triggerhelper.tabs.ActionsTab;
import triggerhelper.triggerCreator;

public class ActionsController {

    //instance variables
    private ActionsTab theTab;
    private triggerCreator theParent;
    private Trigger currentTrigger;

    private JToggleButton bHelp;
    private JTextArea tActions;

    //constructor
    public ActionsController(ActionsTab a, triggerCreator b) {
        theTab = a;
        theParent = b;
        setEasyAccessFields(a);
        setListeners();
    }

    private void setEasyAccessFields(ActionsTab a) {
        bHelp = a.getbHelp();
        tActions = a.gettAct();
    }

    private void setListeners() {
        bHelp.addActionListener(new java.awt.event.ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                help();
            }
        });
        theTab.addComponentListener(new java.awt.event.ComponentAdapter() {
            @Override
            public void componentShown(java.awt.event.ComponentEvent evt) {
                loadPane();
            }
            @Override
            public void componentHidden(java.awt.event.ComponentEvent evt) {
                saveActions();
            }
        });
    }

    private void saveActions() {
        //save text to current trigger
        currentTrigger.setActions(tActions.getText());
    }

    private void loadPane() {
        currentTrigger = (Trigger)theParent.getCurrentTrigger();
        String actions = currentTrigger.getActions();
        tActions.setText(actions);
        tActions.setCaretPosition(actions.length());
    }

    private void help() {
        throw new UnsupportedOperationException("Not yet implemented");
    }
}
